## This code takes our reweighted survey data as inputs and reshapes it for conjoint analysis. It should be run 2nd (after "raking"). 

#### clear workspace,  load R libraries ####
rm(list=ls())
library(car)
library(ggplot2)
library(reshape2)
library(plyr)
library(survey)
library(tidyverse)
library(arm)
library(Cairo)
library(ggthemes)
library(modelr)
library(sparsereg)
library(viridis)
library(ggpubr)
library(zipcode)
library(stargazer)
library(broom)
library(xtable)
library(grid)
library(gridExtra)
library(RColorBrewer)
library(lmtest)


## set your working directory here
setwd('~/Dropbox (Yale_FES)/ypccc_parrish/GND/replication')

vcovCluster <- function(
  model,
  cluster
)
{
  require(sandwich)
  require(lmtest)
  if(nrow(model.matrix(model))!=length(cluster)){
    stop("check your data: cluster variable has different N than model")
  }
  M <- length(unique(cluster))
  N <- length(cluster)           
  K <- model$rank   
  if(M<50){
    warning("Fewer than 50 clusters, variances may be unreliable (could try block bootstrap instead).")
  }
  dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
  uj  <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
  rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
  return(rcse.cov)
}


#### read df in ####
df.gnd<-read.csv('gnd_weighted.csv')
## rename weight variable
names(df.gnd)[grepl("weights",names(df.gnd))==TRUE]<-"weight"
df.gnd2<-df.gnd

## proportion of respondents sampled before 6/21
df.gnd2<-df.gnd2%>%
  dplyr::mutate(date=as.character(StartDate))%>%
  dplyr::mutate(date=substr(date,start=6,stop=10))%>%
  dplyr::mutate(date=as.numeric(gsub("-","",date)))
nrow(df.gnd2[df.gnd2$date<=621,])/nrow(df.gnd2)
head(df.gnd2$date)

## median complete time
median(df.gnd2$Duration..in.seconds.,na.rm=T) ## 828

df.gnd.con2<-read.csv('gnd_weighted_conjoint2.csv')
names(df.gnd.con2)[grepl("weights",names(df.gnd.con2))==TRUE]<-"weight"
####
v<-read.csv('GND Conjoint_June 15, 2019_18.02.csv',stringsAsFactors=FALSE)
qns<-v[1,]
qns<-t(qns)


# 
zipcounty <- read.csv("zipcounty.csv")
zipcounty$zip <- as.character(zipcounty$zcta5)
zipcounty$state <- as.character(zipcounty$state)

df.gnd <- left_join(df.gnd, zipcounty, by="zip")
df.gnd.con2 <- left_join(df.gnd.con2, zipcounty, by="zip")

#### add state and region variables ####
regions<-read.csv('census_regions.csv')
reg<-regions[grep("Region",regions$Name),]%>%
  dplyr::select(Region,Name)
names(reg)<-c("region_number","region_name")
st<-regions[-grep("Region",regions$Name),]
st<-st[-grep("Division",st$Name),]%>%
  dplyr::select(Region,Name)
names(st)<-c("region_number","state")
st<-left_join(st,reg,by="region_number")
st$region_name<-as.character(st$region_name)
states<-read.csv('state_codes.csv')
head(states)
head(st)
states<-full_join(states,st,by="state")%>%
  dplyr::select(abb,state,region_name,fips)
names(states)<-c("state","state_fullname","region_name","fips")
unique(df.gnd$state)
df.gnd$state<-as.integer(df.gnd$state)
df.gnd<-left_join(df.gnd,states,by=c("state"="fips"))
table(df.gnd$region_name)

df.gnd.con2$state<-as.integer(df.gnd.con2$state)
df.gnd.con2<-left_join(df.gnd.con2,states,by=c("state"="fips"))
table(df.gnd.con2$region_name)
commuter <- read.csv("ruralurbancodes2013.csv")
commuter$metro <- NA
commuter$metro[commuter$RUCC_2013==1] <- "urban"
commuter$metro[commuter$RUCC_2013==2] <- "urban"
commuter$metro[commuter$RUCC_2013==3] <- "urban"
commuter$metro[commuter$RUCC_2013==4] <- "rural"
commuter$metro[commuter$RUCC_2013==5] <- "rural"
commuter$metro[commuter$RUCC_2013==6] <- "rural"
commuter$metro[commuter$RUCC_2013==7] <- "rural"
commuter$metro[commuter$RUCC_2013==8] <- "rural"
commuter$metro[commuter$RUCC_2013==9] <- "rural"
commuter$Fipco <- commuter$FIPS

df.gnd <- left_join(df.gnd, commuter, by="Fipco")
df.gnd.con2 <- left_join(df.gnd.con2, commuter, by="Fipco")

#### prepare data for conjoint analysis ####
## stack data frames
## 6 rows per respondent (2 candidates x 3 vignettes)
## creating rows for each package that each respondent was shown
v<-read.csv('GND Conjoint_June 15, 2019_18.02.csv',stringsAsFactors=FALSE)

names(df.gnd)[grep("out.s_",names(df.gnd))]<-
  gsub("out.s - ","",v[1,grep("out.s_",names(v))])
names(df.gnd)[grep("out.t_",names(df.gnd))]<-
  gsub("out.t - ","",v[1,grep("out.t_",names(v))])
names(df.gnd)[grep("out.u_",names(df.gnd))]<-
  gsub("out.u - ","",v[1,grep("out.u_",names(v))])
names(df.gnd.con2)[grep("out.m_",names(df.gnd.con2))]<-
  gsub("out.m - ","",v[1,grep("out.m_",names(v))])
names(df.gnd.con2)[grep("out.n_",names(df.gnd.con2))]<-
  gsub("out.n - ","",v[1,grep("out.n_",names(v))])
names(df.gnd.con2)[grep("out.o_",names(df.gnd.con2))]<-
  gsub("out.o - ","",v[1,grep("out.o_",names(v))])
names(df.gnd)[grep("weight",names(df.gnd))]<-"weight"
names(df.gnd.con2)[grep("weight",names(df.gnd.con2))]<-"weight"

table(df.gnd$mobile)
# df.gnd<-df.gnd[df.gnd$mobile=="No",]
demos<-c("race_Q","age_Q","gender_Q","PID","income","region_name","Duration..in.seconds.")
# setdiff(c("ResponseId","weight",demos,grep("econ1.S",names(df.gnd))),names(df.gnd))
cols1 = df.gnd[,c("ResponseId","weight",demos,grep("econ1.S",names(df.gnd),value=TRUE),
                  grep("social1.S",names(df.gnd),value=TRUE),
                  grep("carbon1.S",names(df.gnd),value=TRUE),
                  grep("cost1.S",names(df.gnd),value=TRUE),
                  grep("size1.S",names(df.gnd),value=TRUE),
                  grep("sponsor1.S",names(df.gnd),value=TRUE),"conjoint.s","benefits")]
cols2 = df.gnd[,c("ResponseId","weight",demos,grep("econ2.S",names(df.gnd),value=TRUE),
                  grep("social2.S",names(df.gnd),value=TRUE),
                  grep("carbon2.S",names(df.gnd),value=TRUE),
                  grep("cost2.S",names(df.gnd),value=TRUE),
                  grep("size2.S",names(df.gnd),value=TRUE),
                  grep("sponsor2.S",names(df.gnd),value=TRUE),
                  "conjoint.s","benefits")]
cols3 = df.gnd[,c("ResponseId","weight",demos,grep("econ1.T",names(df.gnd),value=TRUE),
                  grep("social1.T",names(df.gnd),value=TRUE),
                  grep("carbon1.T",names(df.gnd),value=TRUE),
                  grep("cost1.T",names(df.gnd),value=TRUE),
                  grep("size1.T",names(df.gnd),value=TRUE),
                  grep("sponsor1.T",names(df.gnd),value=TRUE),
                  "conjoint.t","benefits")]
cols4 = df.gnd[,c("ResponseId","weight",demos,grep("econ2.T",names(df.gnd),value=TRUE),
                  grep("social2.T",names(df.gnd),value=TRUE),
                  grep("carbon2.T",names(df.gnd),value=TRUE),
                  grep("cost2.T",names(df.gnd),value=TRUE),
                  grep("size2.T",names(df.gnd),value=TRUE),
                  grep("sponsor2.T",names(df.gnd),value=TRUE),
                  "conjoint.t","benefits")]
cols5 = df.gnd[,c("ResponseId","weight",demos,grep("econ1.U",names(df.gnd),value=TRUE),
                  grep("social1.U",names(df.gnd),value=TRUE),
                  grep("carbon1.U",names(df.gnd),value=TRUE),
                  grep("cost1.U",names(df.gnd),value=TRUE),
                  grep("size1.U",names(df.gnd),value=TRUE),
                  grep("sponsor1.U",names(df.gnd),value=TRUE),
                  "conjoint.u","benefits")]
cols6 = df.gnd[,c("ResponseId","weight",demos,grep("econ2.U",names(df.gnd),value=TRUE),
                  grep("social2.U",names(df.gnd),value=TRUE),
                  grep("carbon2.U",names(df.gnd),value=TRUE),
                  grep("cost2.U",names(df.gnd),value=TRUE),
                  grep("size2.U",names(df.gnd),value=TRUE),
                  grep("sponsor2.U",names(df.gnd),value=TRUE),
                  "conjoint.u","benefits")]

cols7 = df.gnd.con2[,c("ResponseId","weight",demos,grep("carbon1.M",names(df.gnd.con2),value=TRUE),
                  grep("legacy1.M",names(df.gnd.con2),value=TRUE),
                  grep("energy1.M",names(df.gnd.con2),value=TRUE),
                  grep("invest1.M",names(df.gnd.con2),value=TRUE),
                  grep("cost1.M",names(df.gnd.con2),value=TRUE),
                  grep("size1.M",names(df.gnd.con2),value=TRUE),
                  grep("sponsor1.M",names(df.gnd.con2),value=TRUE),
                  "conjoint.m","benefits")]
cols8 = df.gnd.con2[,c("ResponseId","weight",demos,grep("carbon2.M",names(df.gnd.con2),value=TRUE),
                  grep("legacy2.M",names(df.gnd.con2),value=TRUE),
                  grep("energy2.M",names(df.gnd.con2),value=TRUE),
                  grep("invest2.M",names(df.gnd.con2),value=TRUE),
                  grep("cost2.M",names(df.gnd.con2),value=TRUE),
                  grep("size2.M",names(df.gnd.con2),value=TRUE),
                  grep("sponsor2.M",names(df.gnd.con2),value=TRUE),
                  "conjoint.m","benefits")]
cols9 = df.gnd.con2[,c("ResponseId","weight",demos,grep("carbon1.N",names(df.gnd.con2),value=TRUE),
                  grep("legacy1.N",names(df.gnd.con2),value=TRUE),
                  grep("energy1.N",names(df.gnd.con2),value=TRUE),
                  grep("invest1.N",names(df.gnd.con2),value=TRUE),
                  grep("cost1.N",names(df.gnd.con2),value=TRUE),
                  grep("size1.N",names(df.gnd.con2),value=TRUE),
                  grep("sponsor1.N",names(df.gnd.con2),value=TRUE),
                  "conjoint.n","benefits")]
cols10 = df.gnd.con2[,c("ResponseId","weight",demos,grep("carbon2.N",names(df.gnd.con2),value=TRUE),
                   grep("legacy2.N",names(df.gnd.con2),value=TRUE),
                   grep("energy2.N",names(df.gnd.con2),value=TRUE),
                   grep("invest2.N",names(df.gnd.con2),value=TRUE),
                   grep("cost2.N",names(df.gnd.con2),value=TRUE),
                   grep("size2.N",names(df.gnd.con2),value=TRUE),
                   grep("sponsor2.N",names(df.gnd.con2),value=TRUE),
                   "conjoint.n","benefits")]
cols11 = df.gnd.con2[,c("ResponseId","weight",demos,grep("carbon1.O",names(df.gnd.con2),value=TRUE),
                   grep("legacy1.O",names(df.gnd.con2),value=TRUE),
                   grep("energy1.O",names(df.gnd.con2),value=TRUE),
                   grep("invest1.O",names(df.gnd.con2),value=TRUE),
                   grep("cost1.O",names(df.gnd.con2),value=TRUE),
                   grep("size1.O",names(df.gnd.con2),value=TRUE),
                   grep("sponsor1.O",names(df.gnd.con2),value=TRUE),
                   "conjoint.o","benefits")]
cols12 = df.gnd.con2[,c("ResponseId","weight",demos,grep("carbon2.O",names(df.gnd.con2),value=TRUE),
                   grep("legacy2.O",names(df.gnd.con2),value=TRUE),
                   grep("energy2.O",names(df.gnd.con2),value=TRUE),
                   grep("invest2.O",names(df.gnd.con2),value=TRUE),
                   grep("cost2.O",names(df.gnd.con2),value=TRUE),
                   grep("size2.O",names(df.gnd.con2),value=TRUE),
                   grep("sponsor2.O",names(df.gnd.con2),value=TRUE),
                   "conjoint.o","benefits")]

## policy selection variable
cols1$selected = ifelse(cols1$conjoint.s=="Policy Package A",1,0)
cols2$selected = ifelse(cols2$conjoint.s=="Policy Package B",1,0)
cols3$selected = ifelse(cols3$conjoint.t=="Policy Package A",1,0)
cols4$selected = ifelse(cols4$conjoint.t=="Policy Package B",1,0)
cols5$selected = ifelse(cols5$conjoint.u=="Policy Package A",1,0)
cols6$selected = ifelse(cols6$conjoint.u=="Policy Package B",1,0)

cols7$selected = ifelse(cols7$conjoint.m=="Policy Package A",1,0)
cols8$selected = ifelse(cols8$conjoint.m=="Policy Package B",1,0)
cols9$selected = ifelse(cols9$conjoint.n=="Policy Package A",1,0)
cols10$selected = ifelse(cols10$conjoint.n=="Policy Package B",1,0)
cols11$selected = ifelse(cols11$conjoint.o=="Policy Package A",1,0)
cols12$selected = ifelse(cols12$conjoint.o=="Policy Package B",1,0)


#### standardize names and stack data ####

names(cols1) <- c("id","weight",demos,"Economic","Social","Carbon","Cost","Size","Sponsor","choice","benefits","selected")
names(cols2) <- c("id","weight",demos,"Economic","Social","Carbon","Cost","Size","Sponsor","choice","benefits","selected")
names(cols3) <- c("id","weight",demos,"Economic","Social","Carbon","Cost","Size","Sponsor","choice","benefits","selected")
names(cols4) <- c("id","weight",demos,"Economic","Social","Carbon","Cost","Size","Sponsor","choice","benefits","selected")
names(cols5) <- c("id","weight",demos,"Economic","Social","Carbon","Cost","Size","Sponsor","choice","benefits","selected")
names(cols6) <- c("id","weight",demos,"Economic","Social","Carbon","Cost","Size","Sponsor","choice","benefits","selected")

v1<-rbind(cols1,cols2)%>%arrange(id)%>%dplyr::mutate(vignette_id=1)
v2<-rbind(cols3,cols4)%>%arrange(id)%>%dplyr::mutate(vignette_id=2)
v3<-rbind(cols5,cols6)%>%arrange(id)%>%dplyr::mutate(vignette_id=3)

names(cols7)<-c("id","weight",demos,"Carbon","Legacy","Energy","Investments","Cost","Size","Transportation","choice","benefits","selected")
names(cols8)<-c("id","weight",demos,"Carbon","Legacy","Energy","Investments","Cost","Size","Transportation","choice","benefits","selected")
names(cols9)<-c("id","weight",demos,"Carbon","Legacy","Energy","Investments","Cost","Size","Transportation","choice","benefits","selected")
names(cols10)<-c("id","weight",demos,"Carbon","Legacy","Energy","Investments","Cost","Size","Transportation","choice","benefits","selected")
names(cols11)<-c("id","weight",demos,"Carbon","Legacy","Energy","Investments","Cost","Size","Transportation","choice","benefits","selected")
names(cols12)<-c("id","weight",demos,"Carbon","Legacy","Energy","Investments","Cost","Size","Transportation","choice","benefits","selected")
v4<-rbind(cols7,cols8)%>%arrange(id)%>%dplyr::mutate(vignette_id=1)
v5<-rbind(cols9,cols10)%>%arrange(id)%>%dplyr::mutate(vignette_id=2)
v6<-rbind(cols11,cols12)%>%arrange(id)%>%dplyr::mutate(vignette_id=3)


stack1<-do.call(rbind,list(v1,v2,v3))
stack1<-stack1%>%arrange(id)

length(unique(stack1$id))*6==nrow(stack1)

### recode variables to add "none" category and make factor ####

stack1<-stack1%>%mutate_if(is.factor,as.character)
table(stack1$Economic)
stack1$Economic[stack1$Economic==""]<-"none"
stack1$Economic<-revalue(stack1$Economic,c("none"="none",
                                         "Retraining for all fossil fuel workers"="retrain fossil fuel workers",
                                         "Create millions of high-quality union jobs in the clean energy sector"="unionized clean energy jobs",
                                         "Guaranteed job for any American who wants one"="job guarantee"))
stack1$Economic<-factor(stack1$Economic,levels=c("none","retrain fossil fuel workers","unionized clean energy jobs","job guarantee"))
table(stack1$Economic)
stack1$Social[stack1$Social==""]<-"none"
table(stack1$Social)
stack1$Social<-plyr::revalue(stack1$Social,c("none"="none",
                                             "$15 an hour minimum wage"="$15 minimum wage",
                                             "Environmentally-friendly affordable housing"="affordable housing",
                                             "National, government-run health insurance"="health insurance",
                                             "Free college tuition"="free college"))
table(stack1$Social)
stack1$Social<-factor(stack1$Social,
                      levels=c("none","$15 minimum wage","affordable housing","health insurance","free college"))
stack1$Carbon[stack1$Carbon=="No carbon tax"]<-"none"
table(stack1$Carbon)
stack1$Carbon<-plyr::revalue(stack1$Carbon,c("none"="none",
                                             "Carbon tax, with revenues distributed as monthly payments to all Americans"="tax and dividend",
                                             "Carbon tax, with revenues invested in clean energy infrastructure"="tax and invest",
                                             "Carbon tax, with revenues used to reduce income taxes for all Americans"="revenue neutral tax"))
stack1$Carbon<-factor(stack1$Carbon,levels=c("none","tax and dividend","revenue neutral tax","tax and invest"))
table(stack1$Carbon)
table(stack1$Cost)
stack1$Cost<-factor(stack1$Cost)
stack1$Size<-factor(stack1$Size)
levels(stack1$Size)
table(stack1$Sponsor)
stack1$Sponsor<-plyr::revalue(stack1$Sponsor,c("Democrats"="Democrats","Democrats and some Republicans"="bipartisan"))
stack1$Sponsor<-factor(stack1$Sponsor,levels=c("Democrats","bipartisan"))
levels(stack1$Sponsor)
unique(stack1$benefits)
stack1$treatment<-NA
stack1$treatment[stack1$benefits==sort(unique(stack1$benefits))[1]]<-"none"
stack1$treatment[stack1$benefits==sort(unique(stack1$benefits))[2]]<-"jobs"
stack1$treatment[stack1$benefits==sort(unique(stack1$benefits))[3]]<-"economy"
stack1$treatment[stack1$benefits==sort(unique(stack1$benefits))[4]]<-"justice"
stack1$treatment[stack1$benefits==sort(unique(stack1$benefits))[5]]<-"weather"
stack1$treatment[stack1$benefits==sort(unique(stack1$benefits))[6]]<-"deaths"
stack1$treatment<-factor(stack1$treatment,
                        levels=c("none","jobs","economy","justice","weather","deaths"))

### second conjoint
stack2<-do.call(rbind,list(v4,v5,v6))
stack2<-stack2%>%arrange(id)
length(unique(stack2$id))*6==nrow(stack2)

names(stack2)
stack2<-stack2%>%mutate_if(is.factor,as.character)
table(stack2$Carbon)
stack2$Carbon[stack2$Carbon=="No carbon tax"]<-"none"
stack2$Carbon<-plyr::revalue(stack2$Carbon,c("none"="no carbon tax",
                                             "Carbon tax, with revenues distributed as monthly payments to all Americans"="tax and dividend",
                                             "Carbon tax, with revenues invested in clean energy infrastructure"="tax and invest",
                                             "Carbon tax, with revenues used to reduce income taxes for all Americans"="revenue neutral tax"))
stack2$Carbon<-factor(stack2$Carbon,levels=c("no carbon tax","tax and dividend","revenue neutral tax","tax and invest"))
unique(stack2$Carbon)
table(stack2$Carbon)
table(stack2$Legacy)
stack2$Legacy[stack2$Legacy==""]<-"no fossil infrastructure policy"
stack2$Legacy<-plyr::revalue(stack2$Legacy, 
                             c("no fossil infrastructure policy"="no fossil infrastructure policy",
                               "Eliminate government subsidies for fossil fuels"="eliminate subsidies",
                               "Pay companies to shut down coal plants"="coal plant securitization",
                               "Prosecute fossil fuel companies for promoting false information about climate change"="prosecute"))
stack2$Legacy=factor(stack2$Legacy,levels=c("no fossil infrastructure policy","eliminate subsidies","coal plant securitization","prosecute"))
table(stack2$Legacy)
table(stack2$Energy)
stack2$Energy[stack2$Energy==""]<-"no energy policy"
stack2$Energy<-plyr::revalue(stack2$Energy,
                             c("no energy policy"="no energy policy",
                               "All coal-fired power plants shut down by 2030"="mandate coal phaseout 2030",
                               "All electricity comes from clean coal, natural gas, solar, wind and hydro power by 2040"="CES with CCS",
                               "All electricity comes from nuclear, solar, wind, and hydro power by 2040"="CES with nuclear",
                               "All electricity comes from solar, wind, and hydro power by 2040"="CES renewables only"))
stack2$Energy<-factor(stack2$Energy,levels=c("no energy policy","mandate coal phaseout 2030","CES renewables only",
                                             "CES with nuclear","CES with CCS"))
unique(stack2$Investments)
stack2$Investments[stack2$Investments==""]<-"no investments"
stack2$Investments<-plyr::revalue(stack2$Investments,c("no investments"="no investments",
                                       "Fund a program to make buildings more energy-efficient"="building retrofits",
                                       "Invest in technologies that remove carbon pollution from the air and store it underground"=
                                         "direct air capture",
                                       "Pay farmers to store carbon in their soils and forests"="natural carbon storage"))
stack2$Investments<-factor(stack2$Investments,levels=c("no investments","building retrofits","direct air capture","natural carbon storage"))
stack2$Cost[stack2$Cost==""]<-"none"
stack2$Cost<-factor(stack2$Cost)
levels(stack2$Cost)
stack2$Size<-factor(stack2$Size)
table(stack2$Transportation)
stack2$Transportation[stack2$Transportation==""]<-"no transportation policy"
stack2$Transportation<-plyr::revalue(stack2$Transportation,c("no transportation policy"="no transportation policy",
                                             "Invest billions per year in public transit"="public transit",
                                             "Require that only electric cars can be sold starting in 2030"="electric car mandate"))
stack2$Transportation<-factor(stack2$Transportation,levels=c("no transportation policy","electric car mandate","public transit"))
stack2$treatment<-NA
stack2$treatment[stack2$benefits==sort(unique(stack2$benefits))[1]]<-"none"
stack2$treatment[stack2$benefits==sort(unique(stack2$benefits))[2]]<-"jobs"
stack2$treatment[stack2$benefits==sort(unique(stack2$benefits))[3]]<-"economy"
stack2$treatment[stack2$benefits==sort(unique(stack2$benefits))[4]]<-"justice"
stack2$treatment[stack2$benefits==sort(unique(stack2$benefits))[5]]<-"weather"
stack2$treatment[stack2$benefits==sort(unique(stack2$benefits))[6]]<-"deaths"
stack2$treatment<-factor(stack2$treatment,
                         levels=c("none","jobs","economy","justice","weather","deaths"))

## Respondent - Vignette ID
stack1$resp_vignette_id = paste(stack1$id,stack1$vignette_id,sep="")
stack2$resp_vignette_id = paste(stack2$id,stack2$vignette_id,sep="")

stack1<-dplyr::mutate(stack1,selected.wt=weight*selected)
stack2<-dplyr::mutate(stack2,selected.wt=weight*selected)

write.csv(stack2,"gnd_stack2.csv")
write.csv(stack1,"gnd_stack1.csv")


#### demographic cross-tab tables ####
demotab<-df.gnd%>%
  dplyr::select(gender_Q,age_Q,race_Q,PID,income,region_name,weight)
levels(demotab$race_Q)
demotab$race_Q<-factor(demotab$race_Q,levels(demotab$race_Q)[c(6,2,3,1,4,5)])
racetabw<-questionr::wtd.table(demotab$race_Q,weight=demotab$weight)
racetabw<-data.frame(round(prop.table(racetabw)*100,digits=0))
racetab<-data.frame(round(prop.table(table(demotab$race_Q))*100,0))
racetab2<-data.frame(table(demotab$race_Q))
racetab<-full_join(racetab,racetabw,by="Var1")%>%full_join(racetab2)
names(racetab)<-c("Variable","% (unweighted)","% (weighted)","N (unweighted)")

levels(demotab$age_Q)
agetabw<-questionr::wtd.table(demotab$age_Q,weight=demotab$weight)
agetabw<-data.frame(round(prop.table(agetabw)*100,digits=0))
agetab2<-data.frame(table(demotab$age_Q))
agetab<-data.frame(round(prop.table(table(demotab$age_Q))*100,0))
agetab<-full_join(agetab,agetabw,by="Var1")%>%full_join(agetab2)
names(agetab)<-c("Variable","% (unweighted)","% (weighted)","N (unweighted)")

levels(demotab$gender_Q)
gendertabw<-questionr::wtd.table(demotab$gender_Q,weight=demotab$weight)
gendertabw<-data.frame(round(prop.table(gendertabw)*100,digits=0))
gendertab<-data.frame(round(prop.table(table(demotab$gender_Q))*100,0))
gendertab2<-data.frame(table(demotab$gender_Q))
gendertab<-full_join(gendertab,gendertabw,by="Var1")%>%full_join(gendertab2)
names(gendertab)<-c("Variable","% (unweighted)","% (weighted)","N (unweighted)")

levels(demotab$PID)
demotab$PID<-factor(demotab$PID,levels(demotab$PID)[c(1,4,2,3)])
pidtabw<-questionr::wtd.table(demotab$PID,weight=demotab$weight)
pidtabw<-data.frame(round(prop.table(pidtabw)*100,digits=0))
pidtab<-data.frame(round(prop.table(table(demotab$PID))*100,0))
pidtab2<-data.frame(table(demotab$PID))
pidtab<-full_join(pidtab,pidtabw,by="Var1")%>%full_join(pidtab2)
names(pidtab)<-c("Variable","% (unweighted)","% (weighted)","N (unweighted)")

levels(demotab$income)
demotab$income<-factor(demotab$income,levels(demotab$income)[c(1,3,2)])
incometabw<-questionr::wtd.table(demotab$income,weight=demotab$weight)
incometabw<-data.frame(round(prop.table(incometabw)*100,digits=0))
incometab<-data.frame(round(prop.table(table(demotab$income))*100,0))
incometab2<-data.frame(table(demotab$income))
incometab<-full_join(incometab,incometabw,by="Var1")%>%full_join(incometab2)
names(incometab)<-c("Variable","% (unweighted)","% (weighted)","N (unweighted)")

unique(demotab$region_name)
demotab$region_name<-gsub(" Region","",demotab$region_name)
regiontabw<-questionr::wtd.table(demotab$region_name,weight=demotab$weight)
regiontabw<-data.frame(round(prop.table(regiontabw)*100,digits=0))
regiontab<-data.frame(round(prop.table(table(demotab$region_name))*100,0))
regiontab2<-data.frame(table(demotab$region_name))
regiontab<-full_join(regiontab,regiontabw,by="Var1")%>%full_join(regiontab2)
names(regiontab)<-c("Variable","% (unweighted)","% (weighted)","N (unweighted)")

totals<-data.frame("Total Sample",100,100,nrow(df.gnd))
names(totals)<-names(regiontab)

demovars<-do.call(rbind,list(totals,racetab,gendertab,agetab,incometab,pidtab,regiontab))
print(xtable(demovars,digits=0),include.rownames=FALSE,hline.after=c(-1,0,1,7,9,15,18,22,26))

